package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.CollectBean;
import com.example.administrator.yuejian_2.bean.TrainDetailBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainDetailActivity extends AppCompatActivity {
    private int train_id;
    private int user_id;
    private TextView train_name;
    private ImageView train_pic;
    private TextView train_intro;
    private TextView train_notice;
    private TextView train_time;
    private TextView train_address;
    private TextView train_money;
    private TextView train_tel;
    private Button train_enter;
    private Context context;
    private ImageButton collect_button;
    private int state;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
        Intent intent =getIntent();
        train_id = intent.getIntExtra("train_id", -1);
        train_name = (TextView) findViewById(R.id.t_name);
        train_pic = (ImageView) findViewById(R.id.t_pic);
        train_intro = (TextView) findViewById(R.id.t_intro);
        train_notice = (TextView) findViewById(R.id.notice);
        train_time = (TextView) findViewById(R.id.time);
        train_address = (TextView) findViewById(R.id.add);
        train_money = (TextView) findViewById(R.id.count);
        train_tel = (TextView) findViewById(R.id.t_tel);
        train_enter = (Button) findViewById(R.id.enter);
        back = (ImageButton) findViewById(R.id.back);
        collect_button = (ImageButton) findViewById(R.id.train_collect);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gettraindetail();
        initEvents();
        getState(user_id,train_id);
        collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击收藏按钮 改变收藏状态
                getState(user_id,train_id);
                if (state == 1){
                    //getState(user_id,train_id);
                    deleteCollect(user_id,train_id);
                    getState(user_id,train_id);
                }

                else {
                    addCollect(user_id,train_id);
                    getState(user_id,train_id);
                }
            }
        });
    }
    private void initEvents(){
        train_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();//intent跳转
                finish();//关闭当前界面
                intent.setClass(TrainDetailActivity.this, TrainEnterActivity.class);
                intent.putExtra("train_id",train_id);
                intent.putExtra("train_name",train_name.getText());
                intent.putExtra("train_money",train_money.getText());
                intent.putExtra("train_notice",train_notice.getText());
                startActivity(intent);

            }
        }
        );

    }
    private void getState(int u_id,int t_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<CollectBean> call = service.getstate(u_id,t_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    collect_button.setImageResource(R.drawable.ic_collected);
                    state = 1;

                }
                else {
                    collect_button.setImageResource(R.drawable.ic_collect);
                    state = 0;
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }

        });
    }


    private void addCollect(int u_id, int t_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<CollectBean> call = service.addcollect(u_id,t_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(TrainDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    collect_button.setImageResource(R.drawable.ic_collected);
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }


        });

    }

    private void deleteCollect(int u_id, int t_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<CollectBean> call = service.deletecollect(u_id,t_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(TrainDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                    collect_button.setImageResource(R.drawable.ic_collect);
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }


        });

    }


    public void gettraindetail() {
        context = TrainDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<TrainDetailBean> call = service.detail(train_id);
        call.enqueue(new Callback<TrainDetailBean>() {
            @Override
            public void onResponse(Call<TrainDetailBean> call, Response<TrainDetailBean> response) {
                if (response.body()!=null){
                    train_name.setText(response.body().getCourse_name());
                    train_intro.setText(response.body().getCourse_intro());
                    train_notice.setText(response.body().getCourse_intro_train());
                    train_time.setText( response.body().getCourse_time());
                    train_address.setText(response.body().getCourse_location());
                    train_money.setText(response.body().getCourse_money());
                    train_tel.setText(response.body().getCourse_tel());

                    String tpic = response.body().getCourse_pic();
                    if (!tpic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/clubphoto/"+tpic)
                                .into(train_pic);


                }
            }

            @Override
            public void onFailure(Call<TrainDetailBean> call, Throwable t) {

            }
        });

   }
}